#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/sched.h>	// mm_struct
#include <linux/mm_types.h>
#include <linux/cgroup.h>
#include <linux/syscalls.h>	// sys_close
#include <kernel/fork.c>

MODULE_LICENSE("EPT");
MODULE_AUTHOR("Quentin");
MODULE_DESCRIPTION("TCPSERVER");

//EXPORT_UNUSED_SYMBOL(initmm);

//extern unsigned long end_code;
struct   mm_struct mm;

int init_module(void)
{
  unsigned long *ptr;
  unsigned long arr[4];
  int		i;
  void*		sys_call_table = NULL;

  mm_init(mm);
  printk("--- Loading custom module ---");
  ptr = (unsigned long *)((mm.end_code + 4) & 0xfffffffc);
  while ((unsigned long)ptr < (unsigned long)mm.end_data)
    {
      if (*ptr == (unsigned long *)sys_close)
	{
	  printk(KERN_INFO "================\n");
	  printk(KERN_INFO "Founded at %x\n", ptr);
	  printk(KERN_INFO "================\n");

	  for (i = 0; i < 4; ++i)
	    {
	      arr[i] = ptr[i];
	      arr[i] = (arr[i] >> 16) & 0x0000ffff;
	    }

	  if (arr[0] != arr[2] || arr[1] != arr[3])
	    {
	      sys_call_table = (ptr - __NR_close);
	      break;
	    }
	}
      ptr++;
    }
  printk(KERN_INFO "sys_call_table base found at: %x\n", sys_call_table);
  return 0;
}

void            cleanup_module()
{
  printk("---Unloading custom module---");
}
